import json
import logging

from common.bankcard import db as cards
from common.mch import db as merchant
from common.range import db as range
from common.utils.decorator import response_wrapper
from django.utils.decorators import method_decorator
from django.utils.encoding import smart_unicode
from django.views.generic import TemplateView
from common.admin.model import ROLE
from django.views.decorators.http import require_GET, require_POST

_LOGGER = logging.getLogger('hydra')

class RangeView(TemplateView):
    def get(self, request):
        data = request.GET.dict()
        card = data.get('card_code')

        items = range.list_range(data)
        result_list, result = range.get_range_dict(items, data, card)
        return {"list": result_list, 'page': data.get('$page', 1),
                'size': len(result_list), 'total_count': len(result)}

    @method_decorator(response_wrapper)
    # @method_decorator(token_required)
    def dispatch(self, *args, **kwargs):
        return super(RangeView, self).dispatch(*args, **kwargs)

@require_POST
@response_wrapper
def add_range(request):
    _LOGGER.info('add_range: %s', request.body)
    data = json.loads(request.body)
    result = range.insert_range(data)
    result = result.as_dict()
    return {'id': result['id']}

@require_POST
@response_wrapper
def edit_range(request):
    _LOGGER.info('edit_range: %s', request.body)
    data = json.loads(request.body)
    result = range.update_range(data, data['id'])
    result = result.as_dict()
    return {"data": result['id']}

@require_POST
@response_wrapper
def edit_status_range(request):
    _LOGGER.info('edit_status_range: %s', request.body)
    data = json.loads(request.body)
    result = range.update_range_status(data, data['id'])
    result = result.as_dict()
    return {"data": result['id']}

@require_POST
@response_wrapper
def assign_cards_companies(request):
    _LOGGER.info('assign_cards_company: %s', request.body)
    data = json.loads(request.body)
    result = range.assign_cards_to_range(data)
    # result.as_dict()
    return result

class CardsView(TemplateView):
    def get(self, request):
        data = cards.get_cards_only()
        return data

    @method_decorator(response_wrapper)
    # @method_decorator(token_required)
    def dispatch(self, *args, **kwargs):
        return super(CardsView, self).dispatch(*args, **kwargs)

class RangeCodeView(TemplateView):
    def get(self, request):
        data = range.get_range_only()
        return data

    @method_decorator(response_wrapper)
    # @method_decorator(token_required)
    def dispatch(self, *args, **kwargs):
        return super(RangeCodeView, self).dispatch(*args, **kwargs)

class CompanyView(TemplateView):
    def get(self, request):
        data = merchant.get_company_only()
        return data

    @method_decorator(response_wrapper)
    # @method_decorator(token_required)
    def dispatch(self, *args, **kwargs):
        return super(CompanyView, self).dispatch(*args, **kwargs)

class DepositListView(TemplateView):
    def get(self, request):
        data = request.GET.dict()
        cards = range.list_order_deposit(data['card'])
        return {"list": cards, "size": len(cards)}

    @method_decorator(response_wrapper)
    # @method_decorator(token_required)
    def dispatch(self, *args, **kwargs):
        return super(DepositListView, self).dispatch(*args, **kwargs)

class WithdrawListView(TemplateView):
    def get(self, request):
        data = request.GET.dict()
        cards = range.list_order_withdraw(data['card'])
        return {"list": cards, "size": len(cards)}

    @method_decorator(response_wrapper)
    # @method_decorator(token_required)
    def dispatch(self, *args, **kwargs):
        return super(WithdrawListView, self).dispatch(*args, **kwargs)